﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using Diploma.Models;
using Diploma.Models.Entities;

namespace DiplomaWeb.Models.Student
{
    public class StudentViewModel
    {
        #region Private Fields

        private string levelName;
        private string lecturerPractice;
        private string theme;
        private string lecturerDiplom;
        private string groupName;
        private string fio;
        private string practicePlaceName;
        private string note;
        private string consultant;

        #endregion

        public Guid Id { get; set; }

        public string FIO
        {
            get
            {
                if (groupName == null)
                    groupName = String.Empty;
                return fio;
            }
            set { fio = value; }
        }

        public Guid GroupId { get; set; }
        public string GroupName
        {
            get
            {
                if (groupName == null)
                    groupName = String.Empty;
                return groupName;
            }
            set { groupName = value; }
        }

        public int LevelId { get; set; }

        public string LevelName
        {
            get
            {
                if (levelName == null)
                    levelName = String.Empty;
                return levelName;
            }
            set { levelName = value; }
        }

        public bool HasPracticePlace { get; set; }
        public Guid? PracticePlaceId { get; set; }

        public string PracticePlaceName
        {
            get
            {
                if (practicePlaceName == null)
                    practicePlaceName = String.Empty;
                return practicePlaceName;
            }
            set { practicePlaceName = value; }
        }

        /// <summary>
        /// Lecturer of practice
        /// </summary>
        public string LecturerPractice
        {
            get
            {
                if (lecturerPractice == null)
                    lecturerPractice = String.Empty;
                return lecturerPractice;
            }
            set { lecturerPractice = value; }
        }

        /// <summary>
        /// Diploma theme
        /// </summary>
        public string Theme
        {
            get
            {
                if (theme == null)
                    theme = String.Empty;
                return theme;
            }
            set { theme = value; }
        }

        /// <summary>
        /// Diploma Lecturer
        /// </summary>
        public string LecturerDiplom
        {
            get
            {
                if (lecturerDiplom == null)
                    lecturerDiplom = String.Empty;
                return lecturerDiplom;
            }
            set { lecturerDiplom = value; }
        }

        /// <summary>
        /// Note about student
        /// </summary>
        public string Note
        {
            get
            {
                if (note == null)
                    note = String.Empty;
                return note;
            }
            set { note = value; }
        }

        /// <summary>
        /// Consultant of diploma
        /// </summary>
        public string Consultant
        {
            get
            {
                if (consultant == null)
                    consultant = String.Empty;
                return consultant;
            }
            set { consultant = value; }
        }

        /// <summary>
        /// Number in the order
        /// </summary>
        public int Num { get; set; }

        public StudentViewModel(){}

        public StudentViewModel(Diploma.Models.Student student)
        {
            ToViewModel(student);
        }

        public StudentViewModel(SaveStateOrderPractice student)
        {
            ToViewModel(student);
        }

        private void ToViewModel(SaveStateOrderPractice student)
        {
            Num = student.Num;
            if (student.StudentId.HasValue)
            {
                var studentEntity = DataManager.Instance.StudentsRepository.GetStudent(student.StudentId.Value);
                if (studentEntity != null)
                {
                    ToViewModel(studentEntity);
                }
                else
                {
                    FIO = student.Fio;
                    Note = "У данного студента нет темы на дипломирование";
                }
            }
        }

        public void ToViewModel(Diploma.Models.Student student)
        {
            Id = student.UserId;
            FIO = student.FullName;
            GroupId = student.StudentsGroup.StudentsGroupId;
            GroupName = student.StudentsGroup.Name;
            LevelId = student.StudentsGroup.QualificationLevel.QualificationLevelId;
            LevelName = student.StudentsGroup.QualificationLevel.Name;
            if (student.CollaborationRoom != null)
            {
                HasPracticePlace = student.CollaborationRoom.PracticePlace.HasValue;

                if (student.CollaborationRoom.PracticePlace.HasValue)
                {
                    PracticePlaceId = student.CollaborationRoom.PracticePlace.Value;
                    PracticePlaceName = DataManager.Instance.CompaniesRepository.Get(PracticePlaceId.Value).DisplayName;
                    LecturerPractice = DataManager.Instance.StudentsRepository.GetStudentPracticeLecturer(student.UserId);
                }

                if (student.CollaborationRoom.Consultant != null)
                {
                    Consultant = student.CollaborationRoom.Consultant.FullName;
                }
                Theme = student.CollaborationRoom.BaseTheme;
                LecturerDiplom = student.CollaborationRoom.Lecturer.FullName;
            }
        }

        public StudentEntity ToEntity()
        {
            return new StudentEntity
                       {
                           Id = Id,
                           Consultant = Consultant,
                           FIO = FIO,
                           GroupId = GroupId,
                           GroupName = GroupName,
                           HasPracticePlace = HasPracticePlace,
                           LecturerDiplom = LecturerDiplom,
                           LecturerPractice = LecturerPractice,
                           LevelId = LevelId,
                           LevelName = LevelName,
                           Note = Note,
                           Num = Num,
                           PracticePlaceId = PracticePlaceId,
                           PracticePlaceName = PracticePlaceName,
                           Theme = Theme
                       };
        }
    }
}